<?php

namespace App\Models;

use App\Models\Traits\ProductCacheTrait;
use App\Models\Traits\ProductTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Factories\HasFactory;

/**
 * App\Models\Product
 *
 * @property int $id
 * @property int|null $vendor_id belongs to this vendor
 * @property int|null $category_id belongs to this category
 * @property int|null $brand_id belongs to this brand
 * @property int|null $attribute_group_id belongs to this attribute group
 * @property string $title
 * @property string|null $title_long
 * @property string|null $code product code
 * @property string|null $description
 * @property array|null $recommendation
 * @property string|null $cover cover image
 * @property string|null $price_lowest_cost lowest price cost of all skus
 * @property string|null $price_lowest lowest price of all skus
 * @property string|null $price_market market price
 * @property int $sold_count
 * @property int $comment_count
 * @property int $comment_favor_rate
 * @property bool $is_subsidy
 * @property bool $is_hongkong_direct
 * @property bool $is_overseas_direct
 * @property bool $is_delivery_free
 * @property bool $is_sf
 * @property bool $is_returnable if support returnable service after sale
 * @property bool $is_sale
 * @property bool $is_top
 * @property bool $is_pop strong recommend
 * @property bool $is_rec recommendation
 * @property bool $is_new
 * @property bool $is_hot
 * @property int $order
 * @property \Illuminate\Support\Carbon|null $deleted_at
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read \App\Models\AttributeGroup|null $attributeGroup
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\AttributeValue[] $attributeValues
 * @property-read int|null $attribute_values_count
 * @property-read \App\Models\Brand|null $brand
 * @property-read \App\Models\Category|null $category
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Comment[] $comments
 * @property-read int|null $comments_count
 * @property-read mixed $cover_url
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\HotProduct[] $hotProducts
 * @property-read int|null $hot_products_count
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ProductImage[] $images
 * @property-read int|null $images_count
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ProductSku[] $skus
 * @property-read int|null $skus_count
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ProductSlide[] $slides
 * @property-read int|null $slides_count
 * @property-read \App\Models\Vendor|null $vendor
 * @method static Builder|Product ajaxSelect2($keyword)
 * @method static \Database\Factories\ProductFactory factory(...$parameters)
 * @method static Builder|Product hot()
 * @method static Builder|Product new()
 * @method static Builder|Product newModelQuery()
 * @method static Builder|Product newQuery()
 * @method static \Illuminate\Database\Query\Builder|Product onlyTrashed()
 * @method static Builder|Product pop()
 * @method static Builder|Product query()
 * @method static Builder|Product rec()
 * @method static Builder|Product sale()
 * @method static Builder|Product top()
 * @method static Builder|Product whereAttributeGroupId($value)
 * @method static Builder|Product whereBrandId($value)
 * @method static Builder|Product whereCategoryId($value)
 * @method static Builder|Product whereCode($value)
 * @method static Builder|Product whereCommentCount($value)
 * @method static Builder|Product whereCommentFavorRate($value)
 * @method static Builder|Product whereCover($value)
 * @method static Builder|Product whereCreatedAt($value)
 * @method static Builder|Product whereDeletedAt($value)
 * @method static Builder|Product whereDescription($value)
 * @method static Builder|Product whereId($value)
 * @method static Builder|Product whereIsDeliveryFree($value)
 * @method static Builder|Product whereIsHongkongDirect($value)
 * @method static Builder|Product whereIsHot($value)
 * @method static Builder|Product whereIsNew($value)
 * @method static Builder|Product whereIsOverseasDirect($value)
 * @method static Builder|Product whereIsPop($value)
 * @method static Builder|Product whereIsRec($value)
 * @method static Builder|Product whereIsReturnable($value)
 * @method static Builder|Product whereIsSale($value)
 * @method static Builder|Product whereIsSf($value)
 * @method static Builder|Product whereIsSubsidy($value)
 * @method static Builder|Product whereIsTop($value)
 * @method static Builder|Product whereOrder($value)
 * @method static Builder|Product wherePriceLowest($value)
 * @method static Builder|Product wherePriceLowestCost($value)
 * @method static Builder|Product wherePriceMarket($value)
 * @method static Builder|Product whereRecommendation($value)
 * @method static Builder|Product whereSoldCount($value)
 * @method static Builder|Product whereTitle($value)
 * @method static Builder|Product whereTitleLong($value)
 * @method static Builder|Product whereUpdatedAt($value)
 * @method static Builder|Product whereVendorId($value)
 * @method static \Illuminate\Database\Query\Builder|Product withTrashed()
 * @method static \Illuminate\Database\Query\Builder|Product withoutTrashed()
 * @mixin \Eloquent
 * @property bool $is_spend_section
 * @method static Builder|Product whereIsReturnSection($value)
 * @method static Builder|Product whereIsSpendSection($value)
 * @property bool $is_top_100
 * @method static Builder|Product whereIsTop100($value)
 * @property int $is_gold_bean
 * @method static Builder|Product whereIsGoldBean($value)
 * @property int $is_gold_storm
 * @method static Builder|Product whereIsGoldStorm($value)
 * @method static Builder|Product goldBean()
 * @method static Builder|Product top100()
 */
class Product extends Model
{
    use HasFactory;
    use SoftDeletes;
    use ProductTrait;
    use ProductCacheTrait;

    protected $guarded = ['id'];

	protected $casts = [
		'recommendation'            => 'json',
		'is_subsidy' 	            => 'boolean',
		'is_hongkong_direct' 	    => 'boolean',
		'is_overseas_direct' 	    => 'boolean',
		'is_delivery_free' 	        => 'boolean',
		'is_sf' 	                => 'boolean',
		'is_returnable' 	        => 'boolean',
		'is_sale' 	                => 'boolean',
		'is_top'                    => 'boolean',
		'is_rec'                    => 'boolean',
		'is_new' 	                => 'boolean',
		'is_hot' 	                => 'boolean',
		'is_top_100' 	            => 'boolean',
		'is_gold_bean' 	            => 'boolean',
		'is_gold_storm' 	        => 'boolean',
	];

	protected $appends = [
		'cover_url',
	];

	public function getCoverUrlAttribute()
	{
		return img_product_path($this->cover);
	}

	public function scopeAjaxSelect2(Builder $builder, $keyword)
	{
		return $builder->where('is_sale', true)->where('title', 'like', "%$keyword%");
	}

	public function scopeSale(Builder $builder)
	{
		return $builder->where('is_sale', true);
	}

	public function scopeGroup(Builder $builder)
	{
		return $builder->where('is_group', true);
	}

	public function scopePop(Builder $builder)
	{
		return $builder->where('is_pop', true);
	}

	public function scopeTop(Builder $builder)
	{
		return $builder->where('is_top', true);
	}

	public function scopeGoldBean(Builder $builder)
	{
		return $builder->where('is_gold_bean', true);
	}

	public function scopeTop100(Builder $builder)
	{
		return $builder->where('is_top_100', true);
	}

	public function scopeRec(Builder $builder)
	{
		return $builder->where('is_rec', true);
	}

	public function scopeNew(Builder $builder)
	{
		return $builder->where('is_new', true);
	}

	public function scopeHot(Builder $builder)
	{
		return $builder->where('is_hot', true);
	}

	public function scopeNogold(Builder $builder)
	{
		return $builder->where('is_gold_bean',false)->where("is_gold_storm",false);
	}

	public function vendor()
	{
		return $this->belongsTo(Vendor::class);
	}

	public function category()
	{
		return $this->belongsTo(Category::class);
	}

	public function brand()
	{
		return $this->belongsTo(Brand::class);
	}

	public function attributeGroup()
	{
		return $this->belongsTo(AttributeGroup::class);
	}

	public function attributeValues()
	{
		return $this->hasMany(AttributeValue::class);
	}

	public function skus()
	{
		return $this->hasMany(ProductSku::class);
	}

	public function seckillProducts()
	{
		return $this->hasMany(SeckillProduct::class);
	}

	public function slides()
	{
		return $this->hasMany(ProductSlide::class);
	}

	public function expresses()
	{
		return $this->hasMany(ProductExpress::class);
	}

	public function extraExpresses()
	{
		return $this->hasMany(ProductExtraExpress::class);
	}

	public function images()
	{
		return $this->hasMany(ProductImage::class);
	}

	public function comments()
	{
		return $this->morphMany(Comment::class, 'commentable');
	}

	public function commandUser()
	{
		return $this->belongsTo(User::class,'command_user_id','id');
	}

}
